<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

const formModel = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
});

const rules = {
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value !== formModel.value.newPassword) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ]
};

const formRef = ref(null);

// 提交表单
const submitForm = async () => {
  try {
    await formRef.value.validate();
    
    // 模拟密码修改（实际项目中应调用API）
    if (formModel.value.oldPassword === '741852963') { // 假设原密码是这个（实际应从接口验证）
      ElMessage.success('密码修改成功，请重新登录');
      // 重置表单
      formRef.value.resetFields();
    } else {
      ElMessage.error('原密码输入错误');
    }
  } catch (error) {
    // 表单验证失败
    return;
  }
};
</script>

<template>
  <div class="password-container">
    <el-card>
      <div slot="header">
        <h2>重置密码</h2>
      </div>
      
      <el-form 
        ref="formRef"
        :model="formModel" 
        :rules="rules" 
        label-width="120px"
        class="password-form"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input 
            v-model="formModel.oldPassword" 
            type="password" 
            show-password
          ></el-input>
        </el-form-item>
        
        <el-form-item label="新密码" prop="newPassword">
          <el-input 
            v-model="formModel.newPassword" 
            type="password" 
            show-password
          ></el-input>
        </el-form-item>
        
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input 
            v-model="formModel.confirmPassword" 
            type="password" 
            show-password
          ></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">确认修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.password-container {
  padding: 20px;
  max-width: 600px;
  margin: 0 auto;
}

.password-form {
  margin-top: 20px;
}
</style>